package com.xuzhijie.pms.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuzhijie.pms.common.Constants;
import com.xuzhijie.pms.common.Result;
import com.xuzhijie.pms.entity.*;
import com.xuzhijie.pms.entity.dto.AdministratorDto;
import com.xuzhijie.pms.entity.vo.AdministratorVo;
import com.xuzhijie.pms.exception.PmsServerException;
import com.xuzhijie.pms.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.net.InetAddress;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 许志杰
 * @since 2024-02-05
 */
@RestController
@RequestMapping("/admin")
@Slf4j
public class AdministratorController {

    @Resource
    private IAdministratorService Service;

    @Resource
    private IMoveRequestService iMoveRequestService;

    @Resource
    private IFeedbackService iFeedbackService;

    @Resource
    private IRenovationService iRenovationService;

    @Resource
    private IRepairService iRepairService;

    @GetMapping("/getAdminById")
    public Result getAdminById(@RequestParam String id){
        return Service.getAdminById(id);
    }

    @PostMapping("/changeAdmin")
    public Result changeAdmin(@RequestBody AdministratorDto administratorDto){
        String id = administratorDto.getId();
        System.out.println("id: "+id);
        Administrator one = Service.getById(id);
        one.setFace(administratorDto.getFace());
        one.setIdentificationCard(administratorDto.getIdentificationCard());
        Service.saveOrUpdate(one);
        return Result.success();
    }

    @PostMapping("/changePassword")
    public Result changePassword(@RequestBody Map<String,Object> request){
        return Service.changePassword(request);
    }

    @PostMapping("/login")
    public Result login(@RequestBody AdministratorDto dto){
        return Service.login(dto);
    }


    @GetMapping("/page")
    public Result findPage(@RequestParam Integer page,
                           @RequestParam Integer size,
                           @RequestParam String name,
                           @RequestParam String phone,
                           @RequestParam String status,
                           @RequestParam(required = false) Date startTime,
                           @RequestParam(required = false) Date endTime){
        log.info(String.valueOf(startTime));
        log.info(String.valueOf(endTime));
        return Service.findPage(page,size,name,phone,status,startTime,endTime);
    }

    @PutMapping("/edit")
    public Result editAdmin(@RequestBody AdministratorVo vo){

        return Service.editAdmin(vo);
    }

    @DeleteMapping("/delete")
    public Result deleteAdmin(@RequestBody String[] ids){
        return Service.deleteAdmin(ids);
    }

    @PutMapping("/create")
    public Result createAdmin(@RequestBody Map<String, String> requestBody){
        String name = requestBody.get("name");
        String phone = requestBody.get("phone");
        String password = requestBody.get("password");
        String id = requestBody.get("id");
        String roleId = requestBody.get("roleId");
        System.out.println(requestBody);
        return Service.createAdmin(name,phone,password,id,roleId);
    }
    
    @GetMapping("/feedback-by-day")
    public List<Long> getRecentFBByDay(){
        //获取当前时间
        LocalDateTime now = LocalDateTime.now();
        List<Long> dayCount = new ArrayList<>();
        for (int i = 0; i < 7; i++) {
            LocalDate currentDate = now.minusDays(i).toLocalDate();
            //计算查询时间的起始和结束时间
            LocalDateTime startOfDate = currentDate.atStartOfDay();
            LocalDateTime endOfDate = startOfDate.plusHours(23).plusMinutes(59).plusSeconds(59);
            long count = iFeedbackService
                            .count(new LambdaQueryWrapper<Feedback>()
                            .between(Feedback::getCreateTime, startOfDate, endOfDate));
            dayCount.add(count);
        }
        return dayCount;
    }

    @GetMapping("/moveRequest-by-day")
    public List<Long> getRecentVRByDay(){
        //获取当前时间
        LocalDateTime now = LocalDateTime.now();
        List<Long> dayCount = new ArrayList<>();
        for (int i = 0; i < 7; i++) {
            LocalDate currentDate = now.minusDays(i).toLocalDate();
            //计算查询时间的起始和结束时间
            LocalDateTime startOfDate = currentDate.atStartOfDay();
            LocalDateTime endOfDate = startOfDate.plusHours(23).plusMinutes(59).plusSeconds(59);
            long count = iMoveRequestService
                    .count(new LambdaQueryWrapper<MoveRequest>()
                            .between(MoveRequest::getCreateTime, startOfDate, endOfDate));
            dayCount.add(count);
        }
        return dayCount;
    }

    @GetMapping("/renovation-by-day")
    public List<Long> getRecentrenovationByDay(){
        //获取当前时间
        LocalDateTime now = LocalDateTime.now();
        List<Long> dayCount = new ArrayList<>();
        for (int i = 0; i < 7; i++) {
            LocalDate currentDate = now.minusDays(i).toLocalDate();
            //计算查询时间的起始和结束时间
            LocalDateTime startOfDate = currentDate.atStartOfDay();
            LocalDateTime endOfDate = startOfDate.plusHours(23).plusMinutes(59).plusSeconds(59);
            long count = iRenovationService
                    .count(new LambdaQueryWrapper<Renovation>()
                            .between(Renovation::getCreateTime, startOfDate, endOfDate));
            dayCount.add(count);
        }
        return dayCount;
    }

    @GetMapping("/repair-by-day")
    public List<Long> getrepairByDay(){
        //获取当前时间
        LocalDateTime now = LocalDateTime.now();
        List<Long> dayCount = new ArrayList<>();
        for (int i = 0; i < 7; i++) {
            LocalDate currentDate = now.minusDays(i).toLocalDate();
            //计算查询时间的起始和结束时间
            LocalDateTime startOfDate = currentDate.atStartOfDay();
            LocalDateTime endOfDate = startOfDate.plusHours(23).plusMinutes(59).plusSeconds(59);
            long count = iRepairService
                    .count(new LambdaQueryWrapper<Repair>()
                            .between(Repair::getCreateTime, startOfDate, endOfDate));
            dayCount.add(count);
        }
        return dayCount;
    }

    @GetMapping("/server-info")
    public Map<String,String> getServerInfo() {
        Map<String,String> map = new HashMap<>();

        try {

            InetAddress localhost = InetAddress.getLocalHost();

            String hostName = localhost.getHostName();
            map.put("name",hostName);

            String ipAddress = localhost.getHostAddress();
            map.put("ip",ipAddress);

            // 获取当前项目的端口号
            String port = System.getProperty("server.port");
            map.put("port",port);

            // 获取服务器操作系统
            String osName = System.getProperty("os.name");
            map.put("osName",osName);

            // 获取项目所在的文件夹
            String projectFolder = System.getProperty("user.dir");
            map.put("projectFolder",projectFolder);

            // 获取系统所在文件夹
            String systemFolder = System.getenv("SystemRoot");
            map.put("systemFolder",systemFolder);

        } catch (Exception e) {
            e.printStackTrace();
            throw new PmsServerException("系统错误", Constants.ILLEGAL_PARAMETERS);

        }

        return map;
    }
}

